<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="addEditName + title" :visible.sync="dialogVisible" width="30%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="计划名称" prop="item1">
                <el-input v-model="ruleForm.item1" class="w100p" placeholder="请输入计划名称"></el-input>
            </el-form-item>
            <el-form-item label="计划有效时间" prop="item2">
                <el-date-picker v-model="ruleForm.item13" type="datetimerange" :picker-options="pickerOptions"
                    range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" align="right">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="计划启用停用">
                <el-switch v-model="ruleForm.item4" active-color="#13ce66" inactive-color="#ff4949" :active-value="true"
                    :inactive-value="false" active-text="启用" inactive-text="停用">
                </el-switch>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        let validatorNumber = function (rule, value, callback) {
            if (value > 0) {
                callback()
            } else {
                callback(new Error('请输入大于0的数值！'))
            }
        }
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() < Date.now() - 8.64e7;
                },
                shortcuts: [{
                    text: '一周',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() + 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', [end, start]);
                    }
                }, {
                    text: '一个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() + 3600 * 1000 * 24 * 30);
                        picker.$emit('pick', [end, start]);
                    }
                }, {
                    text: '三个月',
                    onClick(picker) {
                        const end = new Date();
                        const start = new Date();
                        start.setTime(start.getTime() + 3600 * 1000 * 24 * 90);
                        picker.$emit('pick', [end, start]);
                    }
                }]
            },
            addEditName: '添加',
            addEditApi: '',
            ruleForm: {},
            rules: {
                item1: [
                    { required: true, message: '请输入计划名称', trigger: 'change' }
                ],
                num: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
                item8: [
                    { required: true, message: '请选择', trigger: 'change' }
                ],
                item9: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
                materialName: [
                    { required: true, message: '请选择物料', trigger: 'change' }
                ],
                item2: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ],
                item3: [
                    { required: true, message: '请选择批次', trigger: 'change' }
                ],
                item4: [
                    { required: true, message: '请选择养殖设施', trigger: 'change' }
                ],
                item6: [
                    { required: true, message: '请选择作业人', trigger: 'change' }
                ],
                item7: [
                    { required: true, message: '请选择通知时间', trigger: 'change' }
                ],
                item9: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
                item10: [
                    { required: true, message: '请选择作业时间类型', trigger: 'change' }
                ],
                item11: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ],
                item12: [
                    { required: true, message: '请选择作业', trigger: 'change' }
                ],
                item13: [
                    { required: true, message: '请选择时间', trigger: 'change' }
                ],
                item15: [
                    { required: true, message: '请输入任务名称', trigger: 'change' }
                ],
                item16: [
                    { required: true, validator: validatorNumber, trigger: 'change' }
                ],
            }
        }
    },
    computed: {

    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    
    mounted() { },
    methods: {
        initData() {
            if (this.addEditFlag == 'add') {
                this.resetForm()
                if (this.ruleForm.item4 && Array.isArray(this.ruleForm.item4)) {
                    console.log(231531)
                    this.$set(this.ruleForm.item4, 0, [{
                        materialName: null, num: null
                    }])
                } else {
                    console.log(1111)
                    this.$set(this.ruleForm, 'item4', [{
                        materialName: null, num: null
                    }])
                }
                this.addEditApi = ''
                this.addEditName = '添加'
            } else if (this.addEditFlag == 'edit') {
                this.ruleForm = { ...this.data }
                this.addEditApi = ''
                this.addEditName = '编辑'
            }
        },
        addMaterial() {
            let length = this.ruleForm.item4?.length || 0
            if (length) {
                this.$set(this.ruleForm.item4, length, {
                    materialName: null, num: null
                })
            }
        },
        delMaterial(index) {
            let list = this.ruleForm.item4;
            list.splice(index, 1);
            this.$set(this.ruleForm, 'item4', list)
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi.then(res => {
                        this.cancel()
                    }).catch(err => {

                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.line {
    border-bottom: 1px solid #e6e6e6;
}

.icon {
    font-size: 20px;
    line-height: 29px;
    cursor: pointer;
}

::v-deep .noLabel {
    .el-form-item {
        .label {
            display: none;
        }

        .el-form-item__content {
            margin: 0;
        }
    }
}


.elRow {
    text-align: center;
}
</style>
  